package controllayer;

import static org.junit.Assert.*;
import modellayer.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The Class PersonControllerTest.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class PersonControllerTest {

	/** The person co1. */
	controllayer.PersonController personCo1 = new controllayer.PersonController();
	/** The salesman counter. */
	private int salesmanCounter = 1;

	/**
	 * Sets the up.
	 *
	 * @throws Exception
	 *             the exception
	 */
	@Before
	public void setUp() throws Exception {
		personCo1.createCustomer("Ole", "Vesterbro", "Aalborg",
				"olex999@live.dk", "123");
		personCo1.createSalesman("Benja", "Eternitten", "Aalborg",
				"leckcy@gmail.com", "456", 1.0);
		personCo1.createBusinessCustomer("Lazz Slambert", "Rikkevej",
				"Aalborg", "lassesl93@gmail.com", "789", "777", 10.0);
		salesmanCounter = personCo1.getSalesmanID();
	}

	/**
	 * Tear down.
	 *
	 * @throws Exception
	 *             the exception
	 */
	@After
	public void tearDown() throws Exception {
		Customer c = (Customer) personCo1.findCustomer("123");
		personCo1.removePerson(c);
		Salesman s = (Salesman) personCo1.findSalesman(personCo1
				.getSalesmanID() - 1);
		personCo1.removePerson(s);
	}

	/**
	 * Test create customer.
	 */
	@Test
	public void testCreateCustomer() {
		// Positiv test customer
		assertNotNull(personCo1.findCustomer("123"));
		// Negativ test customer
		try {
			personCo1.findCustomer("321");
		} catch (NullPointerException npe) {
			assertEquals("Kunde ikke fundet!", npe.getMessage());
		}
		personCo1.createCustomer("test", "test", "test", "test", "123");
		//assertNotEquals("test", personCo1.findCustomer("123").getName());
		// Positiv test BusinessC
		assertNotNull(personCo1.findBusinessCustomer("777"));
		// Negativ test BusinessC
		try {
			personCo1.findBusinessCustomer("777");
		} catch (NullPointerException npe) {
			assertEquals("Erhverskunde ikke fundet!", npe.getMessage());
		}
		personCo1.createBusinessCustomer("test", "test", "test", "test",
				"test", "777", 10.0);
		//assertNotEquals("test", personCo1.findBusinessCustomer("777"));
		// Positiv test Salesman
		assertNotNull(personCo1.findSalesman(salesmanCounter - 1));
		// Negativ test Salesman
		try {
			personCo1.findSalesman(99999);
		} catch (NullPointerException npe) {
			assertEquals("Sælger ikke fundet!", npe.getMessage());
		}
	}

	/**
	 * Test find customer.
	 */
	@Test
	public void testFindCustomer() {
		// Positiv test customer
		assertNotNull(personCo1.findCustomer("123"));
		// Negativ test customer
		try {
			personCo1.findCustomer("321");
		} catch (NullPointerException npe) {
			assertEquals("Kunde ikke fundet!", npe.getMessage());
		}
		// Positiv test BusinessC
		assertNotNull(personCo1.findBusinessCustomer("777"));
		// Negativ test BusinessC
		try {
			personCo1.findBusinessCustomer("888");
		} catch (NullPointerException npe) {
			assertEquals("Erhvervskunde ikke fundet!", npe.getMessage());
		}
		// Positiv test Salesman
		assertNotNull(personCo1.findSalesman(salesmanCounter - 1));
		// Negativ test Salesman
		try {
			personCo1.findSalesman(99999);
		} catch (NullPointerException npe) {
			assertEquals("Sælger ikke fundet!", npe.getMessage());
		}
	}

	/**
	 * Test update customer.
	 */
	@Test
	public void testUpdateCustomer() {
		// Positiv test customer
		assertNotNull(personCo1.findCustomer("123"));
		Person tempP = personCo1.findCustomer("123");
		personCo1.updateCustomer(tempP, "test", "test", "test", "test", "654");
		assertEquals("test", personCo1.findCustomer("654").getName());
		// Negativ test customer
		try {
			personCo1.findCustomer("321");
		} catch (NullPointerException npe) {
			assertEquals("Kunde ikke fundet!", npe.getMessage());
		}
		personCo1.updateCustomer(tempP, "Ole", "Vesterbro", "Aalborg",
				"olex999@live.dk", "123");

		// Positiv test BusinessC
		assertNotNull(personCo1.findBusinessCustomer("777"));
		Person tempBC = personCo1.findBusinessCustomer("777");
		personCo1.updateBusinessCustomer(tempBC, "test", "test", "test",
				"test", "15615", "888", 10.0);
		assertEquals("test", personCo1.findBusinessCustomer("888").getName());
		// Negativ test BusinessC
		try {
			personCo1.findBusinessCustomer("777");
		} catch (NullPointerException npe) {
			assertEquals("Erhvervskunde ikke fundet!", npe.getMessage());
		}
		personCo1.updateBusinessCustomer(tempBC, "test", "test", "test",
				"test", "test", "888", 10.0);
		//assertNotEquals("Ole", personCo1.findBusinessCustomer("888"));
		// Positiv test Salesman
		assertNotNull(personCo1.findSalesman(salesmanCounter - 1));
		// Negativ test Salesman
		try {
			personCo1.findSalesman(99999);
		} catch (NullPointerException npe) {
			assertEquals("Sælger ikke fundet!", npe.getMessage());
		}
	}

	/**
	 * Test delete customer.
	 */
	@Test
	public void testDeleteCustomer() {
		Customer c = (Customer) personCo1.findCustomer("123");
		personCo1.removePerson(c);
		try {
			personCo1.findCustomer("123");
		} catch (NullPointerException npe) {
			assertEquals("Kunde ikke fundet!", npe.getMessage());
		}
		personCo1.createCustomer("Ole", "Vesterbro", "Aalborg",
				"olex999@live.dk", "123");
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		personCo1.removePerson(s);
		try {
			personCo1.findSalesman(salesmanCounter - 1);
		} catch (NullPointerException npe) {
			assertEquals("Sælger ikke fundet!", npe.getMessage());
		}
		personCo1.createSalesman("Benja", "Eternitten", "Aalborg",
				"leckcy@gmail.com", "456", 1.0);
		Customer bc = (BusinessCustomer) personCo1.findBusinessCustomer("777");
		personCo1.removePerson(bc);
		try {
			personCo1.findBusinessCustomer("777");
		} catch (NullPointerException npe) {
			assertEquals("Erhvervskunde ikke fundet!", npe.getMessage());
		}
		personCo1.createBusinessCustomer("Lazz Slambert", "Rikkevej",
				"Aalborg", "lassesl93@gmail.com", "789", "777", 10.0);

	}
}